package com.woniu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.dto.CarSearchDTO;
import com.woniu.entity.vo.CarListVO;
import com.woniu.mapper.CarImgMapper;
import com.woniu.mapper.CarMapper;
import com.woniu.pojo.Car;
import com.woniu.pojo.CarImg;
import com.woniu.service.ICarService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *
 * @author tarnett
 */
@Service
public class CarServiceImpl implements ICarService {

    @Resource
    private CarMapper carMapper;
    @Resource
    private CarImgMapper carImgMapper;

    @Override
    public PageResult<CarListVO> listSearch(CarSearchDTO carSearchDTO) {
        // 1. 开启分页
        PageHelper.startPage(carSearchDTO.getCurrentPage(), carSearchDTO.getPageSize());
        // 2. 调用mapper查询数据
        Page<CarListVO> page = carMapper.listSearch(carSearchDTO);
        // 3. 封装分页对象
        PageResult<CarListVO> pageResult = new PageResult<>();
        pageResult.setTotal(page.getTotal());
        pageResult.setData(page.getResult());
        // 4. 返回数据
        return pageResult;
    }

    @Override
    public Result save(Car car) {
        // 判断车辆标题是否已经存在
        Car car1 = checkTitleExist(car.getTitle());
        if(car1 != null){
            return Result.error("该名称已经存在");
        }
        // 保存车辆
        Integer row = carMapper.save(car);
        // 保存车辆关联的图片列表
        int i = 1;
        for(String url : car.getCarImgList()){
            CarImg carImg = new CarImg();
            carImg.setCarId(car.getId());
            carImg.setUrl(url);
            carImg.setSort(i++);
            carImgMapper.save(carImg);
        }
        return Result.commonByRow(row);
    }

    @Override
    public Integer deleteByIds(Long[] ids) {
        return carMapper.deleteByIds(ids);
    }

    @Override
    public Result updateById(Car car) {
        // 判断车辆标题是否已经存在
        Car car1 = checkTitleExist(car.getTitle());
        if(car1 != null && !car1.getId().equals(car.getId())){
            return Result.error("该名称已经存在");
        }
        // 先删除所有关联的图片
        carImgMapper.deleteByCarId(car.getId());
        // 修改车辆信息
        Integer row = carMapper.updateById(car);
        // 添加车辆关联的图片列表
        int i = 1;
        for(String url : car.getCarImgList()){
            CarImg carImg = new CarImg();
            carImg.setCarId(car.getId());
            carImg.setUrl(url);
            carImg.setSort(i++);
            carImgMapper.save(carImg);
        }
        return Result.commonByRow(row);
    }

    @Override
    public Car getById(Long id) {
        // 查询车辆信息
        Car car = carMapper.getById(id);
        // 查询车辆关联的图片列表
        List<String> carImgList = carImgMapper.listByCarId(id);
        car.setCarImgList(carImgList);
        return car;
    }

    @Override
    public Car checkTitleExist(String title) {
        return carMapper.checkTitleExist(title);
    }

    @Override
    public Integer updateShowStatus(Long id, String val) {
        return carMapper.updateShowStatus(id, val);
    }
}
